package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.teacher.bean.StudentAnalyInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 出勤详情界面
 * Created by Administrator on 2018/7/3 0003.
 */

public class TeacherAttendanceDetailsActivity extends BaseActivity implements View.OnClickListener, CalendarView.OnYearChangeListener, CalendarView.OnDateSelectedListener {

    private ImageView ivBack;

    //标题
    private TextView tvTitle;
    private ImageView ivLeft;
    private ImageView ivRight;
    private CalendarView calendarView;
    private LinearLayout llContent;
    private RelativeLayout rlLoading;

    //当前的显示日期
    private TextView tvCurrDate;

    private StudentAnalyInfo.DataBean info;

    //选择的开始时间
    private String begindate;

    //选择的结束时间
    private String enddate;

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_attendance_details;
    }

    /**
     * 初始化视图
     */
    @Override
    protected void init() {

        initStateBar();

        //初始化视图
        initView();

        //获取Intent
        initIntent();

        //设置日历控件
        initCandlerData();

    }

    /**
     * 获取Intent对象
     */
    private void initIntent() {
        info = (StudentAnalyInfo.DataBean) getIntent().getSerializableExtra("data");
        begindate = getIntent().getStringExtra("begindate");
        enddate = getIntent().getStringExtra("enddate");
        if (info != null) {
            //设置标题
            tvTitle.setText(info.getName() + "出勤详情");
        }
    }


    /**
     * 初始化日历数据
     */
    private void initCandlerData() {

        rlLoading.setVisibility(View.GONE);
        llContent.setVisibility(View.VISIBLE);

        //获取传递的考勤数据
        List<StudentAnalyInfo.DataBean.DetailsBean> candlerDatas = info.getDetails();

        //第一步:设置日历的日期范围
        //设置最小年份,最小年份对应的月份,最大年份,最大年份对应月份
        calendarView.setRange(Integer.parseInt(getYear(begindate)), Integer.parseInt(getMonth(begindate)), Integer.parseInt(getYear(enddate)), Integer.parseInt(getMonth(enddate)));

        //第二步:设置日历的标记时间
        List<Calendar> schemes = new ArrayList<>();
        if (candlerDatas != null && candlerDatas.size() != 0) {
            for (int i = 0; i < candlerDatas.size(); i++) {
                if ("0".equals(candlerDatas.get(i).getStatus())) {
                    //已到即对应签到
                    schemes.add(getSchemeCalendar(Integer.parseInt(getYear(candlerDatas.get(i).getDate())), Integer.parseInt(getMonth(candlerDatas.get(i).getDate())), Integer.parseInt(getDay(candlerDatas.get(i).getDate())), getResources().getColor(R.color.teacher_cd), "签到"));
                } else if ("1".equals(candlerDatas.get(i).getStatus())) {
                    //迟到
                    schemes.add(getSchemeCalendar(Integer.parseInt(getYear(candlerDatas.get(i).getDate())), Integer.parseInt(getMonth(candlerDatas.get(i).getDate())), Integer.parseInt(getDay(candlerDatas.get(i).getDate())), getResources().getColor(R.color.teacher_qd), "迟到"));
                } else if ("2".equals(candlerDatas.get(i).getStatus())) {
                    //请假
                    schemes.add(getSchemeCalendar(Integer.parseInt(getYear(candlerDatas.get(i).getDate())), Integer.parseInt(getMonth(candlerDatas.get(i).getDate())), Integer.parseInt(getDay(candlerDatas.get(i).getDate())), getResources().getColor(R.color.teacher_kk), "请假"));
                } else if ("3".equals(candlerDatas.get(i).getStatus())) {
                    //旷课
                    schemes.add(getSchemeCalendar(Integer.parseInt(getYear(candlerDatas.get(i).getDate())), Integer.parseInt(getMonth(candlerDatas.get(i).getDate())), Integer.parseInt(getDay(candlerDatas.get(i).getDate())), getResources().getColor(R.color.teacher_qj), "旷课"));
                }
            }
        }

        calendarView.setSchemeDate(schemes);

        //设置初次进入显示的时间
        tvCurrDate.setText(calendarView.getCurYear() + "年" + calendarView.getCurMonth() + "月");
    }

    /**
     * 设置标记的方法
     *
     * @param year
     * @param month
     * @param day
     * @param color
     * @param text
     * @return
     */
    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        ivLeft = findViewById(R.id.iv_left);
        ivLeft.setOnClickListener(this);
        ivRight = findViewById(R.id.iv_right);
        ivRight.setOnClickListener(this);
        tvCurrDate = findViewById(R.id.tv_currdate);
        calendarView = findViewById(R.id.calendarView);
        calendarView.setOnDateSelectedListener(this);
        calendarView.setOnYearChangeListener(this);
        llContent = findViewById(R.id.ll_content);
        llContent.setVisibility(View.GONE);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_left) {
            //左边
            if (null != calendarView) {
                //移动上一个月
                calendarView.scrollToPre();

                if (ivRight.getVisibility() != View.VISIBLE) {
                    ivRight.setVisibility(View.VISIBLE);
                }
            }

        } else if (id == R.id.iv_right) {
            //右边
            if (null != calendarView) {
                calendarView.scrollToNext();
            }

            if (ivLeft.getVisibility() != View.VISIBLE) {
                ivLeft.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 根据返回的时间格式2018-06来获取年
     *
     * @return
     */
    private String getYear(String data) {
        String res = "";
        if (TextUtils.isEmpty(data)) {
            return res;
        } else {
            String[] split = data.split("-");
            if (split != null && split.length != 0) {
                //取出年份
                res = split[0];
            }
            return res;
        }
    }

    /**
     * 根据返回的时间格式2018-06来获取月份,不取月份前面的0
     */
    private String getMonth(String data) {
        String res = "";
        if (TextUtils.isEmpty(data)) {
            return res;
        } else {
            String[] split = data.split("-");
            if (split != null && split.length != 0) {
                //取出月份
                String month = split[1];
                if (month.startsWith("0")) {
                    //以0开头话
                    res = month.substring(1, month.length());
                } else {
                    //不以0开头
                    res = month;
                }
            }
            return res;
        }
    }


    /**
     * 根据返回的时间格式2018-06-13来获取天,不取月份前面的0
     */
    private String getDay(String data) {
        String res = "";
        if (TextUtils.isEmpty(data)) {
            return res;
        } else {
            String[] split = data.split("-");
            if (split != null && split.length != 0) {
                //取出月份
                String day = split[2];
                if (day.startsWith("0")) {
                    //以0开头话
                    res = day.substring(1, day.length());
                } else {
                    //不以0开头
                    res = day;
                }
            }
            return res;
        }
    }

    @Override
    public void onDateSelected(Calendar calendar, boolean isClick) {
        tvCurrDate.setText(calendar.getYear() + "年" + calendar.getMonth() + "月");

        //获取当前月份
        int year = calendar.getYear();

        //获取当前年份
        int month = calendar.getMonth();

        //如果当前年和月等于设置的最小的日历控件则隐藏左边按钮
        if (year == Integer.parseInt(getYear(begindate)) && month == Integer.parseInt(getMonth(begindate))) {
            //隐藏
            ivLeft.setVisibility(View.INVISIBLE);
        } else {
            ivLeft.setVisibility(View.VISIBLE);
        }

        //如果当前年和月等于设置的最小的日历控件则隐藏左边按钮
        if (year == Integer.parseInt(getYear(enddate)) && month == Integer.parseInt(getMonth(enddate))) {
            //隐藏
            ivRight.setVisibility(View.INVISIBLE);
        } else {
            ivRight.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onYearChange(int year) {

    }
}
